﻿using Engine;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System;
using Game.NetWork.Packages;
using Game.NetWork;


namespace Game.Server.Plugins
{
    public class PeaceZoneArea
    {
        public Point3 MinPoint;
        public Point3 MaxPoint;
    }

    public class PeaceZoneZHPlugin : ServerPlugin, ICreatureHealthEventHandle
    {
        public override int Version => 10000;

        public override string Name => "和平区域插件";

        public byte FirstLevel => 0;

        private static string ConfigFilePath => Storage.GetSystemPath(ServerManager.PluginsPath + "/PeaceAreas.json");

        /// <summary>
        /// 和平范围
        /// </summary>
        public static List<PeaceZoneArea> PeaceAreas = new List<PeaceZoneArea>(); 

        /// <summary>
        /// 是否在主城范围内
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public bool IsInMajorCityAreas(Point3 point)
        {
            foreach (var area in PeaceAreas)
            {
                if (area.MinPoint.X <= point.X && area.MinPoint.Y <= point.Y && area.MinPoint.Z <= point.Z && area.MaxPoint.X >= point.X && area.MaxPoint.Y >= point.Y && area.MaxPoint.Z >= point.Z)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            CreatureHealthEventManager.AddObject(this);
        }


        /// <summary>
        /// Project的Load后执行
        /// </summary>
        public override void Load()
        {
            try
            {
                PeaceAreas.Clear();
                if (File.Exists(ConfigFilePath))
                {
                    using (StreamReader file = File.OpenText(ConfigFilePath))
                    {
                        string jsonText = file.ReadToEnd();
                        PeaceAreas = JsonConvert.DeserializeObject<List<PeaceZoneArea>>(jsonText);
                    }
                }
                foreach (var area in PeaceAreas)
                {
                    if (area.MinPoint.X > area.MaxPoint.X)
                    {
                        int temp = area.MaxPoint.X;
                        area.MaxPoint.X = area.MinPoint.X;
                        area.MinPoint.X = temp;
                    }
                    if (area.MinPoint.Y > area.MaxPoint.Y)
                    {
                        int temp = area.MaxPoint.Y;
                        area.MaxPoint.Y = area.MinPoint.Y;
                        area.MinPoint.Y = temp;
                    }
                    if (area.MinPoint.Z > area.MaxPoint.Z)
                    {
                        int temp = area.MaxPoint.Z;
                        area.MaxPoint.Z = area.MinPoint.Z;
                        area.MinPoint.Z = temp;
                    }
                }
            }
            catch(Exception e)
            {
                Log.Error($"插件{Name}装载失败：{e.Message}");
            }
        }

        /// <summary>
        /// Project的Save后执行
        /// </summary>
        public override void Save()
        {
        }

        /// <summary>
        /// 给指定玩家发送提示
        /// </summary>
        /// <param name="componentPlayer"></param>
        /// <param name="text"></param>
        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public bool Heal(ComponentHealth componentHealth, float amount)
        {
            return true;
        }

        public bool Injure(ComponentHealth componentHealth, float amount, ComponentCreature attacker, string cause)
        {
            if (attacker != null && componentHealth != null)
            {
                ComponentPlayer player = attacker.Entity.FindComponent<ComponentPlayer>();
                ComponentPlayer player2 = componentHealth.Entity.FindComponent<ComponentPlayer>();
                if (player != null && player2 != null)
                {
                    if(IsInMajorCityAreas(new Point3(player.ComponentBody.Position)))
                    {
                        ComponentHealth componentHealth2 = player.Entity.FindComponent<ComponentHealth>();
                        if (componentHealth2 != null)
                        {
                            componentHealth2.Injure(0.1f, null, true, "天惩！");
                        }
                        DisplaySmallMessageNet(player, "和平区域不可攻击玩家");
                        return false;
                    }
                }
            }
            return true;
        }
    }
}